package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCartController {



    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        //1、判断当前点的菜，购物车中是否已经存在？
        // select * from shopping_cart
        // where ( dish_id =?  或者 setmeal_id =? ) and user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

        if(shoppingCart.getSetmealId()!=null){
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }

        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        ShoppingCart oneFromSql = shoppingCartService.getOne(qw);
        if(oneFromSql==null){
            //2、如何不存在，没有点过。新增
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else {
            //3、如果存在，说明点过了。数量+1

            oneFromSql.setNumber(oneFromSql.getNumber()+1);
            shoppingCartService.updateById(oneFromSql);
            return R.success(oneFromSql);
        }
    }

    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart){

        Long userId = BaseContext.getCurrentId();
        ShoppingCart one =null;
        // update xxx set number = ?
        // where (dish_id =? 或者 setmeal_id =? ) and user_id = ?
        if(shoppingCart.getDishId()!=null){//菜品
            LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());



            qw.eq(ShoppingCart::getUserId,userId);
            one = shoppingCartService.getOne(qw);
            one.setNumber(one.getNumber()-1);
            //2--> 1  --> 0
            if(one.getNumber()==0){//这个菜我不要了！！
                //delete from xxx where id= ?
                shoppingCartService.removeById(one.getId());
            }else {
                // update
                shoppingCartService.updateById(one);
            }
        }else {//套餐
            LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();

            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

            qw.eq(ShoppingCart::getUserId,userId);
            one = shoppingCartService.getOne(qw);
            one.setNumber(one.getNumber()-1);
            //2--> 1  --> 0
            if(one.getNumber()==0){//这个菜我不要了！！
                //delete from xxx where id= ?
                shoppingCartService.removeById(one.getId());
            }else {
                // update
                shoppingCartService.updateById(one);
            }
        }

        return R.success(one);
    }

    /**
     * 购物车列表查询
     * @return
     */
    @GetMapping("/list")
    public R list(){
        // select * from shoping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R cleanShoppingCart(){
        // 根据当前用户id进行删除：delete from shoping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功！");
    }
}
